﻿namespace Smart.ERP
{
    using System;
    using System.ComponentModel;

    public class EnumUtils
    {
        public static object enumValueOf(string value, Type enumType)
        {
            string[] names = Enum.GetNames(enumType);
            foreach (string str in names)
            {
                if (stringValueOf((Enum) Enum.Parse(enumType, str)).Equals(value))
                {
                    return Enum.Parse(enumType, str);
                }
            }
            throw new ArgumentException("The string is not a description or value of the specified enum.");
        }

        public static string stringValueOf(Enum value)
        {
            DescriptionAttribute[] customAttributes = (DescriptionAttribute[]) value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (customAttributes.Length > 0)
            {
                return customAttributes[0].Description;
            }
            return value.ToString();
        }
    }
}

